package com.pur.zsms.weixinpay.weixinPayCaddemo.web;

import java.io.IOException;
import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pur.zsms.foodmanage.food.service.IFoodService;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.turnover.web.TurnoverController;
import com.pur.zsms.vo.ShoppingCartCashierVo;
import com.pur.zsms.weixinpay.weixinPayCaddemo.wechatscan;
import com.pur.zsms.weixinpay.weixinPayCaddemo.dao.PayDao;

/**
 * 支付控制controller
 * 
 * @author linwenzhou
 * @date 2014-12-08
 */
@Controller
@RequestMapping(value = "/weixinpay")
public class WeixinPayControllerTest {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(TurnoverController.class);
	@Resource(name = "foodService")
	private IFoodService foodService;
	@Resource(name = "shopService")
	private IShopService shopService;

	@RequestMapping(value = "/weixinpay.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addFood(PayDao pd, HttpSession session) throws IOException {
		JSONObject json = new JSONObject();

		// 从数据库取openId 的用户 如果有则直接进入业务界面 没有则进入 用户关联界面
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			pd.setDevice_info(String.valueOf(shopId));

			String a = new BigDecimal(pd.getHtmlValue()).multiply(new BigDecimal(100)).toBigInteger().toString();

			pd.setTotal_fee(a);
			boolean falg = wechatscan.scanpay(pd);
			if (falg) {
				ShoppingCartCashierVo shoppingCartCashierVo = new ShoppingCartCashierVo();
				shoppingCartCashierVo.setCartId(pd.getCartId());
				shoppingCartCashierVo.setShopId(shopId);
				shoppingCartCashierVo.setUserId(userInfo.getUserId());
				shoppingCartCashierVo.setPayWay(2);
				shoppingCartCashierVo.setFoodValueAllCheck(pd.getHtmlValue());
				boolean flag = foodService.receiptCommit(shoppingCartCashierVo);

				json.put("success", true);

			} else {
				json.put("msg", "未知错误！");
				json.put("success", false);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json.toString();

	}
}